﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using hou_elearning.Business;
using hou_elearning.Entity;

public partial class Admin_control_ucThongkequatrinhhoc : System.Web.UI.UserControl
{
    public BaihocEntity oBaihoc = null;
    public KhoahocEntity oKhoahoc = null;
    public List<BaihocEntity> lstBaihoc = null;
    public List<Khoahoc_BaihocEntity> lstKhoahocBaihoc = null;
    public List<Baihoc_SlideEntity> lstBaihocSlide = null;
    public List<SlideEntity> lstSlide = null;
    public SlideEntity oSlide = null;
    public List<ThongketheohocvienEntity> lstThongketheohocvien = null;
    public List<DiemdanhEntity> lstDiemdanh = null;
    public Int64 PK_iNguoidungID = 0;
    public Repeater rptSlide = null;
    public List<TrangslideEntity> lstTrangslide = null;
    RepeaterItem item = null;
    Label lblTongsoluong = null;
    Label lblTongphantram = null;
    int iPhantram = 0;
    string sSoluong = "";
    int iTongsoluong = 0;
    int iTongsoslide = 0;
    short iSodiemdanh = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            napThongke(); //Hiển thị thống kê
            lstBaihoc = new List<BaihocEntity>();
            lstKhoahocBaihoc = new List<Khoahoc_BaihocEntity>();
            lstThongketheohocvien = new List<ThongketheohocvienEntity>();
        }
    }
    /// <summary>
    /// Lấy ID của người dùng
    /// </summary>
    /// <author>liemqv</author>
    /// <create>05/06/2012</create>
    /// 
    public Int64 iGetPK_iNguoidungID()
    {
        Int64 iRes = 0;
        if(Request.QueryString["id"] != null)
        {
            iRes = Int64.Parse(Request.QueryString["id"]);
        }
        else
        {
            if(Session["UserID"] != null)
            {
                iRes = Int64.Parse(Session["UserID"].ToString());
            }
        }
        return iRes;
    }

    /// <summary>
    /// Nạp danh sách khóa học theo người dùng
    /// </summary>
    /// <author>liemqv</author>
    /// <create>05/06/2012</create>
    /// 
    public void napThongke()
    {
        PK_iNguoidungID = iGetPK_iNguoidungID();
        List<Khoahoc_HocvienEntity> lstKhoahocHocvien = Khoahoc_HocvienBRL.GetByFK_iHocvienID(PK_iNguoidungID);
        List<KhoahocEntity> lstKhoahoc = new List<KhoahocEntity>();
        foreach(Khoahoc_HocvienEntity oKhoahocHocvien in lstKhoahocHocvien)
        {
            lstKhoahoc.Add(KhoahocBRL.GetOne(oKhoahocHocvien.FK_iKhoahocID));
        }
        rptKhoahoc.DataSource = lstKhoahoc;
        rptKhoahoc.DataBind();
    }
    protected void rptKhoahoc_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        item = e.Item;
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
        {
            Repeater rptBaihoc = (Repeater)e.Item.FindControl("rptBaihoc");
            oKhoahoc = (KhoahocEntity)e.Item.DataItem;
            lstKhoahocBaihoc = Khoahoc_BaihocBRL.GetByFK_iKhoahocID(oKhoahoc.PK_iKhoahocID);
            lstBaihoc = null;
            lstBaihoc = new List<BaihocEntity>();
            foreach (Khoahoc_BaihocEntity oKhoahocBaihoc in lstKhoahocBaihoc)
            {
                lstBaihoc.Add(BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID));
            }
            rptBaihoc.DataSource = lstBaihoc;
            rptBaihoc.DataBind();
        }
    }
    protected void rptBaihoc_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        item = e.Item;
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
        {
            rptSlide = (Repeater)e.Item.FindControl("rptSlide");
            PK_iNguoidungID = iGetPK_iNguoidungID();
            lstDiemdanh = DiemdanhBRL.GetByFK_iHocvienID(PK_iNguoidungID); //Danh sách điểm danh theo người dùng
            oBaihoc = (BaihocEntity)e.Item.DataItem;
            lstBaihocSlide = Baihoc_SlideBRL.GetByFK_iBaihocID(oBaihoc.PK_iBaihocID);
            lstSlide = null;
            lstSlide = new List<SlideEntity>();
            lstTrangslide = null;
            lstTrangslide = null;
            DiemdanhEntity oDiemdanh = null;
            lstThongketheohocvien = null;
            lstThongketheohocvien = new List<ThongketheohocvienEntity>();
            iSodiemdanh = 0;
            iTongsoluong = 0;
            iTongsoslide = 0;
            foreach (Baihoc_SlideEntity oBaihocSlide in lstBaihocSlide)
            {
                iSodiemdanh = 0;
                oSlide = SlideBRL.GetOne(oBaihocSlide.FK_iSlideID);
                lstSlide.Add(oSlide);
                lstTrangslide = TrangslideBRL.GetByFK_iSlideID(oSlide.PK_iSlideID);
                iTongsoslide += lstTrangslide.Count();
                foreach(TrangslideEntity oTrangslide in lstTrangslide)
                {
                    oDiemdanh = null;
                    oDiemdanh = lstDiemdanh.Find(
                            delegate(DiemdanhEntity oDiemdanhFound)
                            {
                                return oDiemdanhFound.FK_iTrangslideID == oTrangslide.PK_iTrangslideID;
                            }
                        );
                    if(oDiemdanh != null)
                    {
                        ++iSodiemdanh;
                        lstDiemdanh.Remove(oDiemdanh);
                    }
                }
                iTongsoluong += iSodiemdanh;
                iPhantram = 0;
                sSoluong = "";
                if (lstTrangslide.Count() > 0)
                {
                    iPhantram = (iSodiemdanh * 100) / lstTrangslide.Count();
                    sSoluong = iSodiemdanh + "/" + lstTrangslide.Count;
                }
                else
                {
                    iPhantram = 0;
                    sSoluong = "0/0";
                }
                ThongketheohocvienEntity oThongke = new ThongketheohocvienEntity();
                oThongke.sTenslide = oSlide.sTenslide;
                oThongke.sSoluong = sSoluong;
                oThongke.iPhantram = iPhantram;
                lstThongketheohocvien.Add(oThongke);
            }
            //Nạp repeater slide
            rptSlide.DataSource = lstThongketheohocvien;
            rptSlide.DataBind();
            //Hiển thị tổng
            lblTongsoluong = (Label)e.Item.FindControl("lblTongsoluong");
            lblTongphantram = (Label)e.Item.FindControl("lblTongphantram");
            if (iTongsoslide > 0)
            {
                lblTongsoluong.Text = iTongsoluong + "/" + iTongsoslide;
                if ((iTongsoluong * 100) / iTongsoslide < 50)
                {
                    lblTongphantram.Text = "<span class='txt-red'>" + ((iTongsoluong * 100) / iTongsoslide) + "%</span>";
                }
                else
                    if ((iTongsoluong * 100) / iTongsoslide < 70)
                    {
                        lblTongphantram.Text = "<span class='txt-orange'>" + ((iTongsoluong * 100) / iTongsoslide) + "%</span>";
                    }
                else
                    {
                        lblTongphantram.Text = "<span class='txt-blue'>" + ((iTongsoluong * 100) / iTongsoslide) + "%</span>";
                    }
            }
            else
            {
                lblTongsoluong.Text = "0/0";
                lblTongphantram.Text = "0%";
            }
        }
    }
}